package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.Commentapi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManagerService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private VideoApi videoApi;
    @DubboReference
    private Commentapi commentapi;
    @Autowired
    private RedisTemplate redisTemplate;

    public PageResult findAllUsers(Integer page, Integer pagesize) {
        Page<UserInfo> selectpage = userInfoApi.findall(page, pagesize);
        List<UserInfo> userInfoList = selectpage.getRecords();
        for (UserInfo userInfo : userInfoList) {
            if (redisTemplate.hasKey(Constants.FREEZE_USER + userInfo.getId())) {
                userInfo.setUserStatus("2");
            }
        }
        return new PageResult(page, pagesize, selectpage.getTotal(), userInfoList);
    }

    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return userInfo;
    }

    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        PageResult pr = movementApi.findFriendMovement(page, pagesize, uid, state);
        List<Movement> movementList = (List<Movement>) pr.getItems();
        List<Long> userId = CollUtil.getFieldValues(movementList, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userId, null);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : movementList) {
            UserInfo userInfo = infoMap.get(movement.getUserId());
            if (userInfo != null) {
                MovementsVo vo = MovementsVo.init(userInfo, movement);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, pr.getCounts(), vos);
    }

    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
        PageResult pageResult = videoApi.findByUserId(page, pagesize, uid);

        return pageResult;
    }

    public MovementsVo findMovementByid(String id) {
        Movement movement = movementApi.findBymovementId(id);
        UserInfo userInfo = userInfoApi.findById(movement.getUserId());
        if (userInfo != null) {
            MovementsVo movementsVo = MovementsVo.init(userInfo, movement);
            return movementsVo;
        } else {
            return null;
        }
    }

    //根据动态id查询对应动态评论
    public PageResult findcomments(Integer page, Integer pagesize, String messageID) {
        //获取动态评论列表
        List<Comment> comments = commentapi.findComments(messageID, CommentType.COMMENT, page, pagesize);
        List<Long> userId = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userId, null);
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, (long) vos.size(), vos);
    }

    //冻结用户
    public Map userFreeze(Map params) {
       // String userId = (String) params.get("userId");
        Integer userId = Integer.parseInt(params.get("userId").toString());
        Integer freezingTime = Integer.parseInt(params.get("freezingTime").toString());
        //设置k
        String key = Constants.FREEZE_USER + userId;
        Integer days = 0;
        if (freezingTime == 1) {
            days = 3;
        } else if (freezingTime == 2) {
            days = 7;
        }
        String value = JSON.toJSONString(params);
        redisTemplate.opsForValue().set(key, value, days, TimeUnit.MINUTES);
        Map map = new HashMap();
        map.put("message", "冻结成功");
        return map;
    }

    //用户解冻
    public Map userUnfreeze(Map params) {
        Integer userId = Integer.parseInt(params.get("userId").toString());
        redisTemplate.delete(Constants.FREEZE_USER + userId);
        Map map = new HashMap();
        map.put("message", "解冻成功");
        return map;
    }
}
